package com.offcn.user.controller;


import com.offcn.dycommon.enums.AppResponse;
import com.offcn.user.compent.SmsTemplate;
import com.offcn.user.pojo.TMember;
import com.offcn.user.service.UserService;
import com.offcn.user.vo.req.UserRegistVo;
import com.offcn.user.vo.resp.UserRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api(tags = "用户登录/注册")
@Slf4j
public class UserLoginController {

    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserService userService;

    @ApiOperation("获取注册的验证码")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "phone",value = "手机号",readOnly = true)})
    @PostMapping("/sendCode")
    public AppResponse sendCode(String phone){
        String code = UUID.randomUUID().toString().replace("-","").substring(0,6);
        String sendCode = smsTemplate.sendCode(phone);
//        if (StringUtils.isEmpty(sendCode)){
//            AppResponse appResponse = AppResponse.fail(null);
//            appResponse.setMsg("发送失败");
//            return appResponse;
//        }
        stringRedisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        System.out.println(stringRedisTemplate.opsForValue().get(phone));
        AppResponse ok = AppResponse.ok(code);
        ok.setMsg("发送成功");
        return ok;
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public AppResponse regist(UserRegistVo userRegistVo){

        String code = userRegistVo.getCode();//用户输入的验证码
        String redis_code = stringRedisTemplate.boundValueOps(userRegistVo.getLoginacct()).get();

        if(StringUtils.isEmpty(redis_code)){
            //redis中没有验证码
            AppResponse<Object> fail = AppResponse.fail(null);
            fail.setMsg("验证码不存在，请先获取验证码");
            return fail;
        }

        if(!redis_code.equals(code)){
            AppResponse<Object> fail = AppResponse.fail(null);
            fail.setMsg("验证码输入有误");
            return fail;
        }

        TMember member = new TMember();
        BeanUtils.copyProperties(userRegistVo,member);//userRegistVo和member属性名一致的才会拷贝
//        member.setEmail(userRegistVo.getEmail());
//        member.setUserpswd(userRegistVo.getUserpswd());
//        member.setLoginacct(userRegistVo.getLoginacct());

        try {
            userService.registerUser(member);
            AppResponse<Object> ok = AppResponse.ok(null);
            ok.setMsg("注册成功");

            log.debug("用户注册成功，手机号为：{},邮箱：{},验证码：{}",userRegistVo.getLoginacct(),userRegistVo.getEmail(),userRegistVo.getCode());
//            System.out.println();

            return ok;
        } catch (Exception e) {
            e.printStackTrace();
            AppResponse<Object> fail = AppResponse.fail(null);
            fail.setMsg(e.getMessage());
            return fail;
        }
    }

    @ApiOperation("用户登录")
    @ApiImplicitParams(value = {})
    @PostMapping("/login")
    public AppResponse login(String username,String password ){
        TMember member = userService.login(username,password);
        if (member == null){
            AppResponse<UserRespVo> fail = AppResponse.fail(null);
            fail.setMsg("用户名密码错误");
            return fail;
        }

        String token = UUID.randomUUID().toString().replace("-","");
        UserRespVo vo = new UserRespVo();
        BeanUtils.copyProperties(member,vo);
        vo.setAccessToken(token);

        stringRedisTemplate.opsForValue().set(token,member.getId() + "" , 2 ,TimeUnit.HOURS);
        return AppResponse.ok(vo);
    }


}
